import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';

class AddChargePriceSetController extends GetxController {
    List<String> titleStringList = [ResKey.basicInfo.tr,ResKey.siteSettingEffectiveDate.tr,ResKey.siteSettingElectricityStagePrice.tr,];
    final TextEditingController ruleNameController = TextEditingController();
    final DeviceServices services = Get.put(DeviceServices());
    List<dynamic> timeDateList = [
        {
            'effectiveTime':'01-01',
            'expirationTime':'12-31',
        }
    ];
    List<dynamic> stagePriceList = [
        {
            "intervalStartTime": "00:00",
            "intervalEndTime": "24:00",
            'electricityFees':'1',
            'serviceFees':'1'
        }
    ];
    bool isEnableCommit = false;
    int status = 1;
    late int siteId;
    @override
    void onInit() {
    super.onInit();
    siteId = Get.arguments;
    }

    @override
    void onReady() {}

    @override
    void onClose() {}


    ///下一步按钮是否可以点击
    checkCommitStatus() {
        if(ruleNameController.text.isNotEmpty&& timeDateList.isNotEmpty && stagePriceList.isNotEmpty){
            isEnableCommit = true;
        }else{
            isEnableCommit = false;
        }
        Log.d('isEnableCommit==' + isEnableCommit.toString());
        update();
    }

    Future<void> save() async {
        var param = {
            "name": ruleNameController.text,
            "status": status,
            "siteId": siteId,
        };
        if(timeDateList.isNotEmpty){
            param['effectiveTimeList'] = timeDateList;
        }

        if(stagePriceList.isNotEmpty){
            param['hoursPriceList'] = stagePriceList;
        }

        showLoading(toast:ResKey.loading.tr);
        var result = await services.siteChargeAdd(param,onError: (p0) {
            dismissLoading();
            handleException(p0);
            return false;
        },);
        dismissLoading();
        Log.d('result==' + result.toString());
        if(result==true){
            Get.back(result: true);
        }
    }

    void cancelEdit(){
        Get.back();
    }
    void updateTimeList(){
        timeDateList.addAll([ {
            "effectiveTime": "01-01",
            "expirationTime": "12-31"
        }]);
        checkCommitStatus();
        update();
    }

    void deleteTimeList(int index){
        timeDateList.removeAt(index);
        checkCommitStatus();
        update();
    }

    void updateStagePriceList(){
        stagePriceList.addAll([{
            "intervalStartTime": "00:00",
            "intervalEndTime": "24:00",
            "electricityFees": "1",
            "serviceFees": "1"
        }]);
        update();
    }

    void deleteStagePriceList(int index){
        stagePriceList.removeAt(index);
        update();
    }
    ///初始化开始时间从零点开始
    DateTime initStartTime(){
        var now = DateTime.now(); //当前时间
        DateTime date =   DateTime(now.year, now.month, now.day,00,00);
        Log.d('date2==' + date.toString());
        return date;
    }
    ///初始化开始时间23:59结束
    DateTime initEndTime(){
        var now = DateTime.now(); //当前时间
        DateTime date =   DateTime(now.year, now.month, now.day,23,59);
        Log.d('date2==' + date.toString());
        return date;
    }
}
